package com.xh.bussiness.permission.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.permission.bean.dto.UserRoleDto;
import com.xh.bussiness.permission.bean.request.RoleMenuRequest;
import com.xh.bussiness.permission.bean.request.UserRoleRequest;
import com.xh.bussiness.permission.dao.RoleMapper;
import com.xh.bussiness.permission.dao.UserRoleMapper;
import com.xh.bussiness.permission.model.Role;
import com.xh.bussiness.permission.model.RoleMenu;
import com.xh.bussiness.permission.model.UserRole;
import com.xh.bussiness.permission.service.IRoleMenuService;
import com.xh.bussiness.permission.service.IRoleService;
import com.xh.bussiness.permission.service.IUserRoleService;
import com.xh.core.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class UserRoleServiceImpl extends BaseServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private IRoleMenuService roleMenuService;

    @Override
    public List<Map<String, Object>> queryMenuFunctionsByUserId(String userId) {
        return this.dao.queryMenuFunctionsByUserId(userId);
    }

    @Override
    public List<Role> queryRoleByUserId(String userId) {
        QueryWrapper qw = new QueryWrapper<UserRole>();
        qw.eq("user_id", userId);
        List<UserRole> list = this.dao.selectList(qw);
        if (!CollectionUtils.isEmpty(list)) {
            List<Role> roles = roleMapper.selectBatchIds(list.stream().map(item -> item.getRoleId()).collect(Collectors.toList()));

            RoleMenuRequest roleMenuRequest = new RoleMenuRequest();
            for (Role role : roles) {
                roleMenuRequest.setRoleId(role.getId());
                List<RoleMenu> menus = roleMenuService.queryRoleMenu(roleMenuRequest);
                if (!CollectionUtils.isEmpty(menus)) {
                    role.setMenuUrls(menus.stream().map(item -> item.getMenuId()).collect(Collectors.joining(",")));
                }
            }

            return roles;
        }
        return null;
    }

    @Override
    public List<UserRole> queryUserRoleByUserId(String userId) {
        QueryWrapper<UserRole> qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        return this.dao.selectList(qw);
    }


    @Override
    public List queryUserRole(UserRoleRequest bean) {
        QueryWrapper qw = new QueryWrapper<UserRole>();
        //拼装条件
        qw = prepareParams(bean, qw);
        return this.selectList(qw, UserRoleDto.class);
    }


    @Override
    public String addUserRole(UserRoleRequest bean) {
        if (!CollectionUtils.isEmpty(bean.getRoleIds())) {
            for (String roleId : bean.getRoleIds()) {
                UserRole userRole = new UserRole();

                userRole.setUserId(bean.getUserId());
                userRole.setRoleId(roleId);
                this.insert(userRole);
            }

        }
        return null;
    }


    @Override
    public String modifyUserRole(UserRoleRequest bean) {
        this.dao.deleteByUserId(bean.getUserId());
        if (!CollectionUtils.isEmpty(bean.getRoleIds())) {
            for (String roleId : bean.getRoleIds()) {
                UserRole userRole = new UserRole();
                userRole.setUserId(bean.getUserId());
                userRole.setRoleId(roleId);
                this.insert(userRole);
            }
        }
        return null;
    }

    @Override
    public String deleteUserRole(String id) {
        UserRole userRole = this.getById(id);
        if (userRole == null) {
            return "当前用户角色id不存在";
        }
        this.dao.deleteById(id);

        return null;
    }

    @Override
    public boolean deleteByUserId(String userId) {
        return this.dao.deleteByUserId(userId);
    }


    /**
     * 准备条件
     *
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(UserRoleRequest bean, QueryWrapper qw) {
        if (bean.getIds() != null && bean.getIds().length > 0) {
            qw.in("id", bean.getIds());
        }
        if (!StringUtils.isEmpty(bean.getUserId())) {
            qw.eq("user_id", bean.getUserId());
        }
        return qw;
    }
}
